package com.desheng.Controller;


import com.desheng.Service.UserService;
import com.desheng.bean.User;
import com.easyFramework.annotation.Autowired;
import com.easyFramework.annotation.Controller;
import com.easyFramework.annotation.RequestMapping;
import com.easyFramework.annotation.RequestParam;
import com.easyFramework.bean.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/login",method = "post")
    public ModelAndView login(@RequestParam("username") String username,
                              @RequestParam("password") String password, HttpServletRequest request){
        HttpSession httpSession =request.getSession();
        ModelAndView modelAndView = new ModelAndView();
        System.out.println("username=====" + username);
        User user = userService.find(username, password);
        if(user==null){
            modelAndView.addModel("error", "账号或密码错误");
            modelAndView.setPath("index.jsp");
        }else {
            httpSession.setAttribute("user", user);
            System.out.println("登录成功");
            modelAndView.setPath("home.jsp");
        }
        return modelAndView;
    }


}
